package com.excilys.controller;

import java.util.List;

import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.excilys.model.Account;
import com.excilys.model.Logbar;
import com.excilys.model.Transaction;
import com.excilys.service.AccountService;
import com.excilys.service.TransactionService;
import com.excilys.service.TransferFormService;

/**
 * Class that takes care of the user's accounts detailed page
 * 
 * @author excilys dev : GF, SC, NT, YA, VP
 * 
 */
@Controller
@RequestMapping(value = "/accounts/{accountName}")
public class AccountDetailsController {

	@Autowired
	private TransactionService myTransactionService;

	@Autowired
	private AccountService myAccountService;

	Logger logger = LoggerFactory.getLogger(TransferFormService.class);

	/**
	 * Displays the account details page. Display the detail list of accounts
	 * operations with a list of 20 elements by page.
	 * 
	 * http request : GET / jsp response : account-details.jsp
	 * 
	 * <ul>
	 * <li>listDatesLastSixMonth</li>
	 * <li>account</li>
	 * <li>currentDate</li>
	 * <li>cardTransactions : transaction that represents all cards transactions
	 * </li>
	 * <li>transactionsList : list of all transactions</li>
	 * <li>transactionsNoCardList : list of all transactions except cards</li>
	 * <li>transactionsListByPage : list of all other 20 transactions (so,
	 * without cards transactions) list by page</li>
	 * <li>refactoredListCard : refactored list of all card transactions in one
	 * row</li>
	 * </ul>
	 * 
	 * @param accountName
	 * @param year
	 * @param month
	 * @param page
	 */
	@RequestMapping(value = "/{year}/{month}/{page}", method = RequestMethod.GET)
	public String displayAccountDetails(@PathVariable("accountName") String accountName, @PathVariable("year") String year, @PathVariable("month") String month,
			@PathVariable("page") String page, Model model) {

		(new Logbar()).setContent(model);
		Integer intYear = Integer.parseInt(year);
		Integer intMonth = Integer.parseInt(month);

		// GetCurrentDate
		DateTime currentDate = new DateTime(intYear, intMonth, 1, 0, 0, 0, 0);

		// Calls the appropriate services to get the informations related to the
		// chosen account and all the transactions that
		// correspond to this account
		Account account;

		account = myAccountService.getAccountByNameSecured(accountName);

		// List of last 6 months
		DateTime nowDate = new DateTime();
		List<DateTime> dateLastSixMonth = myTransactionService.getDatelastSixMonthsList(nowDate);
		model.addAttribute("listDatesLastSixMonth", dateLastSixMonth);

		// Pagination and Transaction List retrieving
		Integer intPage = Integer.parseInt(page);
		int offset = myTransactionService.getOffset(intPage);

		// Transaction lists
		List<Transaction> transactionsListByPage;
		transactionsListByPage = myTransactionService.getAllTransactionsCheckTransferByAccountIdByMonthAndYear(account.getAccountId(), currentDate, offset);
		List<Transaction> refactoredListTransferByPage;
		refactoredListTransferByPage = myTransactionService.refactorTransferTransactions(transactionsListByPage, account, currentDate);
		List<Transaction> transactionsList = myTransactionService.getAllTransactionsByAccountIdByMonthAndYear(account.getAccountId(), currentDate);
		Transaction refactoredListCard = myTransactionService.refactorCardTransactions(transactionsList, account, currentDate);

		List<Transaction> transactionsNoCardList = myTransactionService.getAllTransactionsCheckTransferByAccountIdByMonthAndYear(account.getAccountId(), currentDate);

		int nbPages = 0;
		nbPages = myTransactionService.getNbPagesAccordingToTransactionListSize(transactionsNoCardList);

		model.addAttribute("account", account);
		model.addAttribute("currentDate", currentDate);
		model.addAttribute("nbPages", nbPages);
		model.addAttribute("page", page);
		model.addAttribute("nbResults", transactionsList.size());

		try {
			model.addAttribute("cardTransactions", refactoredListCard);
			model.addAttribute("transactionsList", refactoredListTransferByPage);

		} catch (Exception e) {
			logger.debug("(!) The transaction list might not contain any operation, thus the system is likely to report an error (m4n-web:AccountDetailsController.java).");
		}

		return "account-details";
	}

	/**
	 * Displays the account details card page. Display the detail list of
	 * accounts card operations with a list of 20 elements by page.
	 * 
	 * http request : GET / jsp response : account-details.jsp
	 * 
	 * <ul>
	 * <li>listDatesLastSixMonth</li>
	 * <li>account</li>
	 * <li>currentDate</li>
	 * <li>transactionsCardList : list of all card transactions per date</li>
	 * <li>transactionsCardListByPage : list of all 20 card transactions per
	 * date by page</li>
	 * </ul>
	 * 
	 * @param accountName
	 * @param year
	 * @param month
	 * @param page
	 */
	@RequestMapping(value = "/card/{year}/{month}/{page}", method = RequestMethod.GET)
	public String displayAccountCardDetails(@PathVariable("accountName") String accountName, @PathVariable("year") String year, @PathVariable("month") String month,
			@PathVariable("page") String page, Model model) {

		(new Logbar()).setContent(model);
		Account account;
		account = myAccountService.getAccountByNameSecured(accountName);
		Integer intYear = Integer.parseInt(year);
		Integer intMonth = Integer.parseInt(month);
		DateTime currentDate = new DateTime(intYear, intMonth, 1, 0, 0, 0, 0);

		// List of last 6 months
		DateTime nowDate = new DateTime();
		List<DateTime> dateLastSixMonth = myTransactionService.getDatelastSixMonthsList(nowDate);

		// Pagination and Transaction List retrieving
		Integer intPage = Integer.parseInt(page);
		int offset = myTransactionService.getOffset(intPage);

		// Transaction lists
		List<Transaction> transactionsCardListByPage;
		transactionsCardListByPage = myTransactionService.getAllTransactionsCardByAccountIdByMonthAndYear(account.getAccountId(), currentDate, offset);
		List<Transaction> transactionsCardList = myTransactionService.getAllTransactionsCardByAccountIdByMonthAndYear(account.getAccountId(), currentDate);

		int nbPages = myTransactionService.getNbPagesAccordingToTransactionListSize(transactionsCardList);

		model.addAttribute("nbPages", nbPages);
		model.addAttribute("page", page);
		model.addAttribute("nbResults", transactionsCardList.size());
		model.addAttribute("listDatesLastSixMonth", dateLastSixMonth);
		model.addAttribute("currentDate", currentDate);
		model.addAttribute("account", account);
		model.addAttribute("transactionsCardList", transactionsCardListByPage);

		return "account-card-details";
	}
}
